package com.lanju.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.lanju.common.SpringUtils;
import com.lanju.entity.Appointment;
import com.lanju.entity.User;
import com.lanju.model.ResultModel;
import com.lanju.service.AppointmentService;
import com.lanju.service.HouseService;
import com.lanju.util.ServletUtil;

@Controller
@RequestMapping("/appointment")
public class AppointmentController {
	private static final Logger LOGGER = LoggerFactory.getLogger(AppointmentController.class);
    
    @Autowired
    AppointmentService appointmentService;
    
	@Autowired
	HouseService houseService;
    
    
    @RequestMapping("/add")
    @ResponseBody
    @SuppressWarnings("unchecked")
    public ResultModel add(HttpServletRequest request, HttpServletResponse response,HttpSession session){
		Map<String, Object> param = ServletUtil.getParameterMap(request);
		JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(param));
		Appointment appointment = json.toJavaObject(Appointment.class);
		 User user =(User) session.getAttribute("userModel");
		appointment.setAppointmentId(UUID.randomUUID().toString());
		appointment.setUserId(user.getUserId());
		appointment.setCreator(user.getUserId());
		appointment.setCreateTime(new Date());
        return  appointmentService.add(appointment,request,response);
    }
    @RequestMapping("/del")
    @ResponseBody
    @SuppressWarnings("unchecked")
    public ResultModel del(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> param = ServletUtil.getParameterMap(request);
		JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(param));
		Appointment appointment = json.toJavaObject(Appointment.class);
        return  appointmentService.del(appointment);
    }
    @RequestMapping("/edit")
    @ResponseBody
    @SuppressWarnings("unchecked")
    public ResultModel edit(HttpServletRequest request, HttpServletResponse response,HttpSession session){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(param));
		Appointment appointment = json.toJavaObject(Appointment.class);
		appointment=appointmentService.getById(appointment.getAppointmentId());
		appointment.setHandlerRemark(param.get("handlerRemark")==null?"":param.get("handlerRemark").toString());
		appointment.setStatus(param.get("status")==null?"":param.get("status").toString());
		User user =(User) session.getAttribute("userModel");
		appointment.setUpdator(user.getUserId());
		appointment.setUpdateTime(new Date());
        return  appointmentService.edit(appointment);
    }
    
    
    @RequestMapping("/getById")
    @ResponseBody
    @SuppressWarnings("unchecked")
    public Map<String,Object> getById(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(param));
		Appointment appointment = json.toJavaObject(Appointment.class);
		Map<String,Object> result = new HashMap<String, Object>();
		appointment = appointmentService.getById(appointment.getAppointmentId());
		result.put("appoint", appointment);
		param.put("houseId", appointment.getHouseId());
		result.put("house", houseService.queryHomeDetailByHouseId(param));
        return  result;
    }
    
    
    /** 根据参数获取 */
    @RequestMapping("/getByParam")
    @ResponseBody
    @SuppressWarnings("unchecked")
    public Appointment getByParam(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(param));
		Appointment appointment = json.toJavaObject(Appointment.class);
        return  appointmentService.getByParam(appointment);
    }
    
    
    /** 获取列表 */
    @RequestMapping("/getList")
    @ResponseBody
    @SuppressWarnings("unchecked")
    public Map<String, Object> getList(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(param));
		Appointment appointment = json.toJavaObject(Appointment.class);
		Integer pageSize = Integer.parseInt( (String)param.get("pageSize") );
		Integer pageNumber = Integer.parseInt( (String)param.get("pageNumber") );
		Integer offset = (pageNumber-1)*pageSize;
        return  appointmentService.getList(param, pageSize, offset);
    }
    @RequestMapping("/delAppointment")
    @ResponseBody
    public Map<String,Object> delAppointment(HttpServletRequest request, HttpServletResponse response){
    	Map<String, Object> param = SpringUtils.getParameterMap(request);
    	return 	appointmentService.delAppointment(param.get("id").toString());
    }
}
